home *** CD-ROM | disk | FTP | other *** search
- /*
- Spectra conversion between LISE and Cessna (OTTO) Format
-
- Since the original FORTRAN routines were unreadable for my small brain
- (approximately 100 lines of real code scatterd on about 20 files and
- coverd under 1000 lines of unusable comments (name of programmer,
- history of file, wether of day and bus connection to the programmers home)
- I have deduced the format empirical from some real spectra.
- For this reason, it may well be, that this routine does not work
- well in all cases.
- */
-
- #include <stdio.h>
- #include <time.h>
- #include <spec.h>
-
- float *spc, *err, *tim;
-
- int swap=FALSE;
- int cessnafd=0;
-
- help()
- {
- printf("CIO Spectra conversion between LISE and CESSNA (OTTO) Format\n");
- printf("Call convention:\n");
- printf(" cio [*.spc] [-l2o cessnafile] [-o2l cessnafile] [-swap] [-o filename]\n");
- printf(" options and theier meaning:\n");
- printf(" -l2o name LISE -> OTTO reads a number of files (*.spc) and\n");
- printf(" converts them into CESSNA format; writes to file 'name'\n");
- printf(" -o2l name OTTO -> LISE read CESSNA 'name', extracts the spectrum\n");
- printf(" supplied as first parameter and generates a LISE spectrum\n");
- printf(" -list name list spectra in CESSNA file name\n");
- printf(" -swap is for integer conversion (680x0<->VAX)\n");
- printf(" -o name may be specified to redirect output of LISE spectra\n\n");
- }
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- int n,m,i,j,k,max;
- char z[80],comment[80];
-
- spc= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
- err= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
- tim= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
-
- if(checkopt(argc,argv,"-swap",z)) swap=TRUE;
- if(checkopt(argc,argv,"-l2o",z)) {
- opencessna(z); /* open cessna file */
- n=1; /* first argument (LISE file) */
- while(1==1) {
- strcpy(z,argv[n++]);
- if(z[0]=='-') break; /* options reached */
- max=readspec(z,spc,err,tim,comment);
- strupc(_spc_0nam);
- cessnawrite(_spc_0nam,max,spc); /* write spectrum in CESSNA format */
- }
- close(cessnafd);
- exit(0);
- }
-
- if(checkopt(argc,argv,"-o2l",z)) {
- cessnafd=open(z,0,0666);
- if(cessnafd<0) {
- strcat(z,".SPC");
- cessnafd=open(z,0,0666);
- }
- if(cessnafd<0) {
- fprintf(stderr,"cio: can not open cessna file >%s<\n",z);
- exit(0);
- }
- strcpy(z,argv[1]); /* get first argument = spectrum name */
- max=cessnaread(z,spc);
- writespec(z,spc,err,max,1,"CESSNA ");
- close(cessnafd);
- exit(0);
- }
-
- if(checkopt(argc,argv,"-list",z)) {
- cessnafd=open(z,0,0666);
- if(cessnafd<0) {
- strcat(z,".SPC");
- cessnafd=open(z,0,0666);
- }
- if(cessnafd<0) {
- fprintf(stderr,"cio: can not open cessna file >%s<\n",z);
- exit(0);
- }
- cessnalist();
- close(cessnafd);
- exit(0);
- }
-
- _help0();
- }
-
- /* -----------------------------------------------------------------
- Writing of data to file (descriptor=cessnafd), including swapping
- ----------------------------------------------------------------- */
- writeb(c)
- unsigned char c;
- {
- write(cessnafd,&c,1);
- }
-
- writei2(i)
- int i;
- {
- int b1,b2;
- unsigned short int b;
-
- b=i;
- if(swap) {
- b1=i>>8 ; b2=i-(b1<<8);
- b = (b2<<8) + b1;
- }
-
- write(cessnafd,&b,2);
- }
-
- writei4(i)
- int i;
- {
- unsigned long int b;
- int b1,b2,b3,b4;
-
- b=i;
- if(swap) {
- b4 = b & 0xff; b = b>>8;
- b3 = b & 0xff; b = b>>8;
- b2 = b & 0xff; b = b>>8;
- b1 = b;
- b = (b4 << 24) + (b3 << 16) + (b2 << 8) + b1;
- }
- write(cessnafd,&b,4);
- }
-
- writestr(str,n)
- char str[];
- int n;
- {
- int i,j,k,l;
-
- l=strlen(str);
- for(i=0;i<l;i++) writeb(str[i]);
- while(i<n) {
- writeb(' '); /* fill rest with blanks (stupid FORTRAN !) */
- i=i+1;
- }
- }
- /* -------------------------------------------------------------------
- Reading of data from file (descriptor=cessnafd), including swapping
- ------------------------------------------------------------------- */
- readb()
- {
- unsigned char c;
- read(cessnafd,&c,1);
- return(c);
- }
-
- readi2()
- {
- int b,b1,b2;
- unsigned short int i;
-
- read(cessnafd,&i,2);
- b=i;
- if(swap) {
- b1=i>>8 ; b2=i-(b1<<8);
- b = (b2<<8) + b1;
- }
- return(b);
- }
-
- readi4()
- {
- unsigned long int i,b;
- int b1,b2,b3,b4;
-
- read(cessnafd,&i,4);
- b=i;
- if(swap) {
- b4 = b & 0xff; b = b>>8;
- b3 = b & 0xff; b = b>>8;
- b2 = b & 0xff; b = b>>8;
- b1 = b;
- b = (b4 << 24) + (b3 << 16) + (b2 << 8) + b1;
- }
- return(b);
- }
-
- readstr(str)
- char str[];
- {
- int i,n;
- char c;
-
- n=0;
- c=0;
- while(c!=32) {
- c=readb();
- str[n++]=c;
- if(c==0) break;
- }
- str[n-1]=0;
- }
-
- /* -------------------------------------------------------------
- generate Header entry
- since there are a lot of ยง"empirical" parameters, i have used
- the variablenames qm1... for these.
- ------------------------------------------------------------- */
- header(name,qm1,qm2,qm3,qm4,fwp,bwp,dirp,hdtyp,spclen,spctyp)
- char name[];
- int qm1,qm2,qm3,qm4,fwp,bwp,dirp,hdtyp,spclen,spctyp;
- {
- int i,n;
- time_t izeit;
- struct tm *now;
- char zeit[80];
-
- izeit=time(NULL); /* get actual time */
- now=localtime(&izeit);
- strftime(zeit,18,"%H:%M %d.%m.%y",now);
-
- writei2(0x1501); /* 1) 21 x I*2 words */
- writei2(256); /* 2) Header length is 256 words */
- writei2(qm1); /* 3) pointer to first free entry */
- writei2(0x17); /* 4) type of header */
- writei2(0x1a); /* 5) pointer to name of file */
- writei2(qm2); /* 6) ?? */
- writei2(0x21); /* 7) pointer to date and time */
- writei2(qm3); /* 8) ?? */
- writei2(qm4); /* 9) ?? */
- writei2(0); /* 10) ?? */
- writei2(0); /* 11) ?? */
- writei2(0); /* 12) ?? */
- writei2(0); /* 13) ?? */
- writei2(0); /* 14) ?? */
- writei2(0); /* 15) ?? */
- writei2(0); /* 16) ?? */
- writei2(0); /* 17) ?? */
- writei2(0); /* 18) ?? */
- writei2(fwp); /* 19) pointer to forward pointer (highly sophisticated) */
- writei2(bwp); /* 20) pointer to backward pointer (highly sophisticated) */
- writei2(dirp); /* 21) pointer to directory */
- writei2(0); /* 22) ?? */
- writei2(0x0201); /* 23) 2 x I*2 words */
- writei2(hdtyp); /* 24) header type */
- writei2(0); /* 25) ?? */
- writei2(0x0c10); /* 26) 12 ASCII */
- writestr(name,12); /* 27-32) file name */
- writei2(0x1210); /* 33) 18 ASCII */
- writestr(zeit,18); /* 34-42 date and time */
- writei2(0); /* 43) ?? */
- switch(hdtyp) {
- case 0x20:
- writei2(0x0202); /* 44) 2 x I*4 words */
- writei4(-1); /* 45-46) forward pointer */
- writei4(-1); /* 47-48) backward pointer */
- writei2(0x0280); /* 49) [I*4 */
- writei2(0x1080); /* 50) [ASCII */
- writei2(0x0c81); /* 51) ]12 */
- writei2(0x1981); /* 52) ]25 */
- /* continues with directory entries */
- break;
- case 0x01:
- writestr("",12); /* 44-59) spaces */
- writei2(0x0501); /* 50) 5 x I*2 words ? */
- writei2(0x0001); /* 51) 1 ? */
- writei2(0x0001); /* 52) 1 ? */
- writei2(spclen); /* 53) spectra length ? */
- writei4(0); /* 54-55) ? */
- writeb(spctyp); /* 56) spectra type ? */
- writeb(spctyp); /* 56) spectra type ? */
- writei2((spclen*spctyp+256)/256);
- /* 57) length of data area ??? */
- writei2(0); /* 58) ?? */
- writei2(0x0100); /* 59) ?? */
- break;
- }
- }
-
-
-
- /* ---------------------------------------------------------------
- open CESSNA file.
- If the file does not exist, then create a new file.
- --------------------------------------------------------------- */
- opencessna(name)
- char name[];
- {
- int n,m,i,j;
-
- cessnafd=open(name,2,0666);
- if(cessnafd<0) {
- printf("creating new CESSNA file\n");
- cessnafd=creat(name,0666);
- header("fuckMMMBAvOE",0x35,0,0,0,0x2c,0,0x31,0x20,0,0);
- close(cessnafd);
- cessnafd=open(name,2,0666);
- }
- }
-
- /* ----------------------------------------------------------------
- write spectrum in CESSNA format
- ---------------------------------------------------------------- */
- cessnawrite(name,max,spc)
- char name[];
- int max;
- float spc[];
- {
- int i,n,m,j,k,l,p,a;
-
- lseek(cessnafd,4L,0); /* position to pointer to next free entry */
- p=readi2(); /* read pointer to next free entry */
- p=2*(p-1); /* construct bytepointer */
- if(p==104) { /* if it is the first entry */
- a=512; /* address of spectrum */
- } else {
- i=p-16; /* pointer to last entry */
- lseek(cessnafd,i,0);
- n=readi2(); /* read entry */
- n = 512 * (n-1); /* construct byte pointer */
- lseek(cessnafd,n+112,0); /* read length of data area */
- l=readi2();
- l = 512 * l; /* construct offset */
- a = n + l;
- }
- i = (a >> 9) + 1; /* construct CESSNA pointer to address */
- lseek_plus(cessnafd,p,0); /* go to next free entry in directory */
- writei2(i); /* write pointer to spectrum */
- writeb(0); /* allways 0 */
- writeb(0x01); /* type of header there */
- writestr(name,12); /* write name of spectrum */
- p=p+16; /* point to next entry */
- p = (p>>1) + 1; /* generate CESSNA pointer */
- lseek_plus(cessnafd,4L,0); /* position to pointer to next free entry */
- writei2(p); /* update pointer */
-
- lseek_plus(cessnafd,a,0); /* point to new spectrum entry */
- header(name,0x41,0x2b,0x32,0xfe,0,0x38,0,0x01,max,2);
- a= a + 506; /* point to data area */
- lseek_plus(cessnafd,a,0);
- writei2(0x8202); /* !!! empirical constant !!! */
- writei2(max); /* write length of data area */
- writei2(0); /* spare ? */
- for(i=0;i<max;i++) { /* write data */
- n = spc[i];
- writei4(n);
- }
- }
-
- /* ----------------------------------------------------------------
- Read spectrum in CESSNA format
- ---------------------------------------------------------------- */
- cessnaread(name,spc)
- char name[];
- float spc[];
- {
- int i,n,m,j,k,l,p,a;
- char s[80];
-
- lseek(cessnafd,40L,0); /* position to pointer to directory */
- p=readi2(); /* read pointer to directory */
- p = 2 * (p-1); /* construct bytepointer */
- p = p + 8;
- n=0;
- while(n<25) {
- lseek(cessnafd,p,0); /* position to directory */
- a=readi2(); /* get address of spectrum */
- i=readi2(); /* skip 2 bytes */
- readstr(s); /* read spectrum name */
- if((strcmp(name,s)==0) && strlen(name)==strlen(s)) { /* entry found */
- a = 512 * (a-1); /* byte address of spectrum */
- lseek(cessnafd,a+104,0); /* position to length entry */
- l = readi2(); /* get length of spectrum */
- i = readi4(); /* skip 4 bytes */
- i = readi2(); /* read type */
- if(i!=0x0202) { /* up to now we can only handle I*4 */
- fprintf(stderr,"cio: sorry, not an I*4 spectrum\n");
- close(cessnafd);
- exit(0);
- }
- lseek(cessnafd,a+512,0); /* go to data */
- for(i=0;i<l;i++) { /* read data */
- m = readi4();
- spc[i] = m;
- }
- return(l);
- }
- n = n + 1;
- p = p + 16;
- }
- fprintf(stderr,"cio: Spectrum >%s< not found\n",name);
- close(cessnafd);
- exit(0);
- }
-
- /* ----------------------------------------------------------------
- List names of spectra in CESSNA file
- ---------------------------------------------------------------- */
- cessnalist()
- {
- int i,n,m,j,k,l,p,a;
- char s[80];
-
- lseek(cessnafd,40L,0); /* position to pointer to directory */
- p=readi2(); /* read pointer to directory */
- p = 2 * (p-1); /* construct bytepointer */
- p = p + 8;
- n=0;
- while(n<25) {
- lseek(cessnafd,p,0); /* position to directory */
- a=readi2(); /* get address of spectrum */
- i=readi2(); /* skip 2 bytes */
- readstr(s); /* read spectrum name */
- a = 512 * (a-1); /* byte address of spectrum */
- lseek(cessnafd,a+104,0); /* position to length entry */
- l = readi2(); /* get length of spectrum */
- i = readi4(); /* skip 4 bytes */
- i = readi2(); /* read type */
- if(strlen(s)>0) printf("%12s %d channels type=%4x ?\n",s,l,i);
- n = n + 1;
- p = p + 16;
- }
- }
-
- lseek_plus(fd,ptr,o)
- int fd,ptr,o;
- {
- int i,n,k,j;
-
- i=lseek(fd,ptr,o);
- if(i!=ptr) {
- i=lseek(fd,0L,2);
- n=ptr-i;
- j=0;
- for(k=0;k<n;k++) write(fd,&j,1);
- i=lseek(fd,ptr,o);
- if(i!=ptr) printf("hier stinkts!");
- }
- return(i);
- }
-
-
-